/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

export class Character {

  /**
   * The minimum value of a
   * <a href="http://www.unicode.org/glossary/#supplementary_code_point">
   * Unicode supplementary code point</a>, constant {@code U+10000}.
   */
  public static readonly MIN_SUPPLEMENTARY_CODE_POINT = 0x010000;

  public static isWhitespace(character: string) {
    return /\s/.test(character);
  }

  public static regionMatches(ignoreCase: boolean, s1: string, s1Start: number, s2: string, s2Start: number, len: number): boolean {
    return ignoreCase ? s1.substr(s1Start, len).toLowerCase() === s2.substr(s2Start, len).toLowerCase() : s1.substr(s1Start, len) === s2.substr(s2Start, len)
  }

  public static isDigit(n: string): boolean {
    return /^\d$/.test(n);
  }

  public static isLetterOrDigit(ch: string): boolean {
    return /^[a-z0-9]*$/i.test(ch);
  }

  public static charCount(codePoint: number): number {
    if (codePoint === undefined || codePoint === null) {
      return 1
    }
    return codePoint >= Character.MIN_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
  }
}

